package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.engine.query.ReturnMetadata;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Attendant;
import ex.com.jm.food.entity.Company;
import ex.com.jm.food.entity.Streets;
import ex.com.jm.food.service.CompanyAttendantService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;
/**
 * 企业下的配送员管理Action层
 * @author zhaojw
 *
 */
public class CompanyAttendantAction extends ActionSupport {

	
	private static final long serialVersionUID = 1L;
	private CompanyAttendantService companyAttendantService;
	private Attendant attendant;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String comId;
	private String attName;
	private Integer status;
	private String order;
	private String sort;
	private String attId;
	private List<Streets> streets=new ArrayList<Streets>();
	public CompanyAttendantService getCompanyAttendantService() {
		return companyAttendantService;
	}
	public void setCompanyAttendantService(
			CompanyAttendantService companyAttendantService) {
		this.companyAttendantService = companyAttendantService;
	}
	public Attendant getAttendant() {
		return attendant;
	}
	public void setAttendant(Attendant attendant) {
		this.attendant = attendant;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public String getAttName() {
		return attName;
	}
	public void setAttName(String attName) {
		this.attName = attName;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getAttId() {
		return attId;
	}
	public void setAttId(String attId) {
		this.attId = attId;
	}
	public List<Streets> getStreets() {
		return streets;
	}
	public void setStreets(List<Streets> streets) {
		this.streets = streets;
	}
	/**
	 * 到达列表页面
	 * @return
	 */
	public String attenList(){
		return "attenList";
	}
	/**
	 * 获取数据
	 */
	public void getAttens(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<Attendant> attendants=this.getCompanyAttendantService().getAttendants(offset, pageRow, comId, attName, status, order, sort);
		JSONArray joatten=JSONArray.fromObject(attendants);
		totalCount=this.getCompanyAttendantService().attCount(comId, attName, status);
		JSONObject jo=JsonUtils.buildJson(joatten, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 编辑信息
	 * @return
	 */
	public String editAtten(){
		if (attId!=null) {
			attendant=this.getCompanyAttendantService().editAttendant(attId);
		}
		Company company=(Company) ActionContext.getContext().getSession().get("company");
		String county=company.getComAddress().split(";")[2];
		streets=this.getCompanyAttendantService().getStreets(county);
		return "edit";
	}
	/**
	 * 添加/更新信息
	 * @return
	 */
	public String saveAtten(){
		if(attendant.getAttName()==null||attendant.getAttName().equals("")){
			addFieldError("nameErroe", "配送员名称不能为空");
			return "edit";
		}
		if(attendant.getAttId()!=null&&!attendant.getAttId().equals("")){
			this.getCompanyAttendantService().updateAtten(attendant);
			return "list";
		}
		attendant.setStatus(0);
		attendant.setCreated(DateUtils.getCurrentDate());
		this.getCompanyAttendantService().saveAtten(attendant);
		return "list";
	}
	
	public String delAtten(){
		this.getCompanyAttendantService().delAtten(attId);
		return "list";
	}
}
